﻿ using Newtonsoft.Json;

namespace 时间段重叠判断
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var existingTimes = new TimeRanges
            {
                TheoryStart = new DateTime(2024, 3, 8),
                TheoryEnd = new DateTime(2024, 3, 8),

                // 没有设置实操时间
            };
            List<DateTime> dateTimes = new List<DateTime>();
            dateTimes.Add(new DateTime(2024, 3, 8));
            dateTimes.Add(new DateTime(2024, 3, 9));
            var S=  Newtonsoft.Json.JsonConvert.SerializeObject(dateTimes);
            if (existingTimes.TheoryStart>existingTimes.TheoryEnd)
            {

            }
            // 第二个计划（有理论和实操考试）
            var newTimes = new TimeRanges
            {
                TheoryStart = new DateTime(2024, 3, 1),
                TheoryEnd = new DateTime(2024, 3, 10),
                PracticeStart = new DateTime(2024, 3, 12),
                PracticeEnd = new DateTime(2024, 3, 15)
            };

            // 检查冲突
            if (newTimes.HasConflictWith(existingTimes, out string conflictMessage))
            {
                Console.WriteLine($"发现时间冲突：{conflictMessage}");
            }
            else
            {
                Console.WriteLine("时间安排正常，无冲突");
            }
        }
    }
    public class TimeRanges
    {
        public DateTime TheoryStart { get; set; }     // 理论考试开始时间（必需）
        public DateTime TheoryEnd { get; set; }       // 理论考试结束时间（必需）
        public DateTime? PracticeStart { get; set; }  // 实操考试开始时间（可选）
        public DateTime? PracticeEnd { get; set; }    // 实操考试结束时间（可选）

        public bool HasConflictWith(TimeRanges other, out string conflictMessage)
        {
            conflictMessage = string.Empty;

            // 检查理论考试时间冲突（必需检查，因为理论时间是必需的）
            if (IsDateRangeOverlapping(TheoryStart, TheoryEnd, other.TheoryStart, other.TheoryEnd))
            {
                conflictMessage = "理论考试时间冲突";
                return true;
            }

            // 如果双方都有实操时间，则检查实操时间冲突
            if (PracticeStart.HasValue && PracticeEnd.HasValue &&
                other.PracticeStart.HasValue && other.PracticeEnd.HasValue)
            {
                if (IsDateRangeOverlapping(PracticeStart.Value, PracticeEnd.Value,
                    other.PracticeStart.Value, other.PracticeEnd.Value))
                {
                    conflictMessage = "实操考试时间冲突";
                    return true;
                }
            }

            // 检查理论和实操的交叉冲突（只在有实操时间的情况下检查）
            if (other.PracticeStart.HasValue && other.PracticeEnd.HasValue)
            {
                if (IsDateRangeOverlapping(TheoryStart, TheoryEnd,
                    other.PracticeStart.Value, other.PracticeEnd.Value))
                {
                    conflictMessage = "理论考试时间与对方实操考试时间冲突";
                    return true;
                }
            }

            if (PracticeStart.HasValue && PracticeEnd.HasValue)
            {
                if (IsDateRangeOverlapping(PracticeStart.Value, PracticeEnd.Value,
                    other.TheoryStart, other.TheoryEnd))
                {
                    conflictMessage = "实操考试时间与对方理论考试时间冲突";
                    return true;
                }
            }

            return false;
        }

        private bool IsDateRangeOverlapping(DateTime startA, DateTime endA, DateTime startB, DateTime endB)
        {
            return startA.Date <= endB.Date && endA.Date >= startB.Date;
        }
    }
}
